package com.lakudouzi.flink.cdc.controller;

import cn.hutool.core.lang.hash.Hash;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.GetResponse;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.lakudouzi.flink.cdc.domain.Contract;
import com.lakudouzi.flink.cdc.domain.ContractDto;
import com.lakudouzi.flink.cdc.domain.Demo2;
import com.lakudouzi.flink.cdc.domain.SystemDept;
import com.lakudouzi.flink.cdc.mapper.ContractMapper;
import com.lakudouzi.flink.cdc.service.ContractService;
import com.lakudouzi.flink.cdc.utils.PageBean;
import com.lakudouzi.flink.cdc.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Array;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@CrossOrigin
@Slf4j
public class EScontroller {
    @Resource
    private ElasticsearchClient client;

    @RequestMapping("addEsDocument")
    public void addEsDocument() throws IOException {
        SystemDept systemDept = new SystemDept();

        systemDept.setName("测试");
        systemDept.setEmail("adad@166.com");
        IndexResponse indexResponse = client.index(i -> i
                .index("system_dept_search")
                .id("100")
                .document(systemDept));

    }


    @RequestMapping("getTempTableData")
    public Result getTempTableData(@RequestBody PageBean<Contract> pageBean) {
        QueryWrapper<Contract> contractQueryWrapper = new QueryWrapper<>();
        contractQueryWrapper.last("limit " + pageBean.getPageSize());
        List<Contract> list = contractService.list(contractQueryWrapper);
        return Result.ok(list);

    }

    @RequestMapping("queryDataBaseInfo")
    public Result queryDataBaseInfo() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("server", "192.168.1.21");
        result.put("port", "3306");



        HashMap<String, Object> datebase = new HashMap<>();
        datebase.put("db01", Arrays.asList("contract", "demo2", "system_dept"));
        datebase.put("demodb", Arrays.asList("ddd", "es_test", "hotel"));
        result.put("databases", datebase);

        result.put("fileds", Arrays.asList("field1", "field2", "field3", "field4", "field5", "field6", "field7", "field8", "field9", "field10", "field11"));
        return Result.ok(result);
    }

    @RequestMapping("tempAddData")
    public void tempAddDate() throws IOException {

//        readData("D:\\WorkSpace\\Java\\flinkcd\\src\\main\\resources\\5657.dat");
        readData("E:\\5747.dat");
    }


    @Resource
    private ContractMapper contractMapper;

    @RequestMapping("processData")
    public Result processData(@RequestBody List<Object> form) throws IOException {
        System.out.println(form);


        HashMap<String, Object> deletemap = new HashMap<>();
        deletemap.put("fields", Arrays.asList("id", "field2"));
        deletemap.put("table", "db01.contract");
        contractMapper.demo("deletemap");
        return Result.ok();

    }
    @Resource
    private ContractService contractService;

    private void readData(String FILE_NAME) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(FILE_NAME), "utf-8"));
        String line;

        while ((line = br.readLine()) != null) {
            // 按行读取
            String[] split = line.split("\\\\N");

            Contract contractDto = new Contract();
            contractDto.setField1(split[0]);
            contractDto.setField2(split[1]);
            contractDto.setField3(split[2]);
            contractDto.setField4(split[3]);
            contractDto.setField5(split[4]);
            contractDto.setField6(split[5]);
            contractDto.setField7(split[6]);
            contractDto.setField8(split[7]);
            contractDto.setField9(split[8]);
            contractDto.setField10(split[9]);
            contractDto.setField11(split[10]);
            contractDto.setField12(split[11]);
            contractDto.setField13(split[12]);
            contractDto.setField14(split[13]);
            contractDto.setField15(split[14]);
            contractDto.setField16(split[15]);
//            contractService.save(contractDto);

        }
        br.close();

    }



    @RequestMapping("highlightQuery")
    public Result highlightQuery() throws IOException {



        SearchResponse<Demo2> response = client.search(g -> g
                        .index("demo2")
                        .query(q ->
                                q.match(t -> t
                                        .field("name")
                                        .query("修改")))
                        .highlight(h->h
                                .fields("name",f->f
                                        .preTags("<em>")
                                        .postTags("</em>"))),
                Demo2.class
        );

        List<Hit<Demo2>> hits = response.hits().hits();

        ArrayList<Demo2> result = new ArrayList<>();
        for (Hit<Demo2> hit : hits) {
            log.info("{}", hit.source());
            Demo2 source = hit.source();
            Map<String, List<String>> highlight = hit.highlight();
            System.out.println(highlight);
            result.add(source);

        }
        return Result.ok(result);
    }


    @RequestMapping("test/time")
    public Result testTime() throws IOException {
        return Result.ok(new Date());
    }




}
